#include <iostream>
#include <vector>
#include <unordered_set>
#include <math.h>
using namespace std;
class Solution
{
public:
    /**
     * @brief
     * 1 <= num <= 10^8
     *
     * @param num
     * @return true
     * @return false
     */
    bool checkPerfectNumber(int num)
    {
        if (1 == num)
        {
            return false;
        }

        unordered_set<int> resSet = {1};
        for (int i = 2; i < sqrt(num); i++)
        {
            if (num % i == 0)
            {
                resSet.emplace(i);
                resSet.emplace(num / i);
            }
        }
        int res = 0;
        for (int r : resSet)
        {
            res += r;
        }

        return res == num;
    }

    bool checkPerfectNumber_2(int num)
    {
        if (num < 2)
        {
            return false;
        }

        int sum = 1;
        for (int i = 2; i * i <= num; i++)
        {
            if (num % i == 0)
            {
                sum += (i == num / i ? i : i + num / i);
            }
            if (sum > num)
            {
                return false;
            }
        }

        return sum == num;
    }

    /**
     * @brief
     * Euclid proved that 2^{p−1}(2^p − 1)2
     * https://leetcode.com/problems/perfect-number/solution/
     * For example, the first four perfect numbers are generated by the formula 2^{p−1}(2^p − 1)2
     */

    int getPerfectNum(int p)
    {
        return (1 << (p - 1)) * ((1 << p) - 1);
    }
    bool checkPerfectNumber_3(int num)
    {
        vector<int> primes = {2, 3, 5, 7, 13, 17, 19, 31};
        for (int prime : primes)
        {
            if (getPerfectNum(prime) == num)
            {
                return true;
            }
        }
        return false;
    }
};
int main()
{
    Solution s;
    cout << s.checkPerfectNumber_3(28) << endl;
    return 0;
}